--with GNAT.IO;
--use GNAT;

--
--    Percentage: Calculate percentage.
--
--       generic fixed point decimal for quantity arguments
--       generic fixed point decimal for results of calculation
--
--       Allows custom precision in percentage calculation.
--

package body Percentages is

   --
   -- Percentage
   --
   --    Calculation percentage.
   --

   --
   -- Delta
   --

   function Percentage( Current_Qty  : in Quantity_Precision_Type;
                        Expected_Qty : in Quantity_Precision_Type )

         return Percentage_Type
   is

      Percentage_Result : Percentage_Type;

   begin

      if( Current_Qty > Expected_Qty ) then

         raise Illegal_Quantity with Qty_Type'Image(Current_Qty)
           & " < "
           & Qty_Type'Image(Expected_Qty );

      end if;

      Percentage_Result := Percentage_Type( ( Current_Qty * 100 ) /
                                              Expected_Qty);

      return Percentage_Result;

   end Percentage;


   --
   -- Integer
   --

   function Percentage ( Current_Qty  : in Quantity_Type_Int;
                         Expected_Qty : in Quantity_Type_Int )

         return Percentage_Type_Int

   is

      Quantity_Delta : constant Quantity_Precision_Type :=

        Quantity_Precision_Type( Current_Qty );

      Expected_Delta : constant Quantity_Precision_Type :=

        Quantity_Precision_Type( Expected_Qty );

      Percentage_Calculated_Delta : Results_Precision_Type;
      Percentage_Calculated_Int   : Percentage_Type_Int;

   begin

      --
      -- convert arguments to delta, use delta version of this function
      --
      --    in this manner, validation logic not duplicated
      --

      Percentage_Calculated_Delta := Percentage( Quantity_Delta,

            Expected_Delta );

      Percentage_Calculated_Int := Percentage_Type_Int(

            Percentage_Calculated_Delta );

      return Percentage_Calculated_Int;

   end Percentage;



end Percentages;